/*
 * FileName：SysDeptCtrl.java 
 * <p>
 * Copyright (c) 2017-2020 <a href="">hermit(1154808491@qq.com)</a>.
 * <p>
 * Licensed under the GNU General Public License, Version 3 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/gpl-3.0.html
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package com.koine.admin.sys.ctrl;

import com.google.common.collect.ImmutableMap;
import com.koine.common.BaseCtrl;
import com.koine.common.Constants;
import com.koine.common.entity.TreeNode;
import com.koine.common.util.AjaxResult;
import com.koine.entity.sys.SysDept;
import com.koine.sys.service.ISysDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 部门
 *
 * @author hermit
 * @version 2.0
 * @date 2018-12-30 22:26:32
 */
@RestController
@RequestMapping("/sys/dept")
@Api(description = "部门")
public class SysDeptCtrl extends BaseCtrl {
    @Resource
    private ISysDeptService sysDeptService;

    /**
     * 部门列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "获取列表信息")
    public AjaxResult list(SysDept sysDept) throws Exception {
        List<SysDept> sysDeptList = sysDeptService.queryList(sysDept);
        return AjaxResult.success(sysDeptList);
    }

    /**
     * 获取部门列表树（ztree）
     */
    @GetMapping("/tree")
    @ApiOperation(value = "获取部门列表树")
    public AjaxResult tree(SysDept sysDept) throws Exception {
        List<SysDept> sysDeptList = sysDeptService.queryList(sysDept);
        //构造ztree
        List<TreeNode> treeNodeList = sysDeptList.parallelStream().map(
                dept -> TreeNode.builder().id(dept.getDeptId())
                        .parentId(dept.getParentId())
                        .name(dept.getDeptName())
                        .attrs(ImmutableMap.of("systemFlag", dept.getSystemFlag()))
                        .isParent(Constants.LEAF_NO.equals(dept.getLeafFlag()))
                        .build()).collect(Collectors.toList());
        return AjaxResult.success(treeNodeList);
    }

    /**
     * 信息
     */
    @GetMapping("/info")
    @ApiOperation(value = "获取详情信息")
    public AjaxResult info(SysDept sysDept) throws Exception {
        SysDept one = sysDeptService.queryObject(sysDept);
        return AjaxResult.success(one);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ApiOperation(value = "保存")
    public AjaxResult save(SysDept sysDept) throws Exception {
        sysDeptService.insert(sysDept);
        return AjaxResult.saveSuccess();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改")
    public AjaxResult update(SysDept sysDept) throws Exception {
        sysDeptService.update(sysDept);
        return AjaxResult.updateSuccess();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @ApiOperation(value = "删除")
    public AjaxResult delete(SysDept sysDept) throws Exception {
        sysDeptService.delete(sysDept);
        return AjaxResult.deleteSuccess();
    }

    /**
     * 批量删除
     */
    @PostMapping("/deleteBatch")
    @ApiOperation(value = "批量删除")
    public AjaxResult deleteBatch(@RequestParam("deptIds[]") String[] deptIds) throws Exception {
        sysDeptService.deleteBatch(Arrays.asList(deptIds));
        return AjaxResult.deleteSuccess();
    }
}
